package com.alibaba.nacos.naming.core.v2.metadata;

import com.alibaba.nacos.common.notify.Event;
import com.alibaba.nacos.common.notify.NotifyCenter;
import com.alibaba.nacos.common.notify.listener.MultiSubscriber;
import com.alibaba.nacos.naming.core.v2.event.client.ClientEvent;
import com.alibaba.nacos.naming.core.v2.event.metadata.MetadataEvent;
import com.alibaba.nacos.naming.core.v2.event.publisher.NamingEventPublisherFactory;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class NamingMetadataManager extends MultiSubscriber {

    private ConcurrentMap<Service, ServiceMetadata> serviceMetadataMap = new ConcurrentHashMap<>();
    private ConcurrentMap<Service, ConcurrentMap<String, InstanceMetadata>> instanceMetadataMap = new ConcurrentHashMap<>();

    @PostConstruct
    void init() {
        NotifyCenter.registerSubscriber(this, NamingEventPublisherFactory.INSTANCE);
    }

    @Override
    public List<Class<? extends Event>> subscribeTypes() {
        return List.of(MetadataEvent.InstanceMetadataEvent.class, MetadataEvent.ServiceMetadataEvent.class, ClientEvent.ClientDisconnectEvent.class);
    }

    @Override
    public void onEvent(Event event) {
        if (event instanceof MetadataEvent.InstanceMetadataEvent instanceMetadataEvent) {

            return;
        }

        if (event instanceof MetadataEvent.ServiceMetadataEvent serviceMetadataEvent) {

            return;
        }

        if (event instanceof ClientEvent.ClientDisconnectEvent clientDisconnectEvent) {
            return;
        }

        throw new UnsupportedOperationException();
    }

    public ServiceMetadata getServiceMetadata(Service service) {
        return serviceMetadataMap.get(service);
    }

    public InstanceMetadata getInstanceMetadata(Service service,
                                                String metadataId) {
        return null;
    }
}
